package org.example.algorithm.doublepointer;

public class MaxAreaSolution {

    public int maxArea(int[] height) {
        int sum = Integer.MIN_VALUE;
        int left = 0;
        int right = height.length - 1;
        while (left < right) {
            sum = Math.max(sum, (right - left) * Math.min(height[left], height[right]));
            if (height[left] > height[right]) {
                right--;
            } else if(height[left] < height[right]) {
                left++;
            } else {
                break;
            }
        }
        return sum;
    }

    public int maxArea2(int[] height) {
        int sum = Integer.MIN_VALUE;
        for (int i=0;i<height.length;i++) {
            if (height[i] == 0) {
                continue;
            }
            for (int j=height.length-1;j>=i+1 && j-i>sum/height[i];j--) {
                sum = Math.max(sum, (j-i) * Math.min(height[i], height[j]));
            }
        }
        return sum > 0 ? sum : 0;
    }
}
